package cn.alcatraz.pet.service.impl;

import cn.alcatraz.basic.utils.BaiduAuditUtils;
import cn.alcatraz.pet.domain.Pet;
import cn.alcatraz.pet.domain.PetDetail;
import cn.alcatraz.pet.domain.PetOnlineAuditLog;
import cn.alcatraz.pet.mapper.PetDetailMapper;
import cn.alcatraz.pet.mapper.PetMapper;
import cn.alcatraz.pet.mapper.PetOnlineAuditLogMapper;
import cn.alcatraz.pet.service.IPetService;
import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alcatraz
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired private PetMapper petMapper;

    @Autowired private PetDetailMapper petDetailMapper;

    @Autowired private PetOnlineAuditLogMapper petOnlineAuditLogMapper;





    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            //得到每个宠物基本信息对象
            Pet pet = petMapper.loadById(id);
            StringBuffer sbf = new StringBuffer();
            //调用百度AI进行审核
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sbf.append("名称不合法，违规名称：" + pet.getName());
            }
           /* if (!BaiduAuditUtils.ImgCensor(BaseConstant.FAST_DFS_IP + pet.getResources().split(",")[0])){
                sbf.append("图片不合法，违规名称：" + pet.getResources().split(",")[0]);
            }*/
            if (sbf.length() > 0){
                //违规，保存审核日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(sbf.toString());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;
            }
            petMapper.onsale(id);
        }
    }

    @Override
    public void delete(Long id) {
        petMapper.delete(id);
        petDetailMapper.deleteByPetId(id);
    }

    @Override
    public void save(Pet pet) {
        petMapper.save(pet);

        PetDetail detail = pet.getDetail();

        detail.setPetId(pet.getId());

        petDetailMapper.save(detail);

    }

    @Override
    public void update(Pet pet) {
        petMapper.update(pet);

        PetDetail detail = pet.getDetail();

        detail.setPetId(pet.getId());

        petDetailMapper.update(detail);
    }
}
